package com.bbzn.user.client.aspect;

import com.bbzn.user.client.constant.MongoDbConstant;
import com.bbzn.user.client.constant.RedisConstant;
import com.bbzn.user.client.dataobject.OptHis;
import com.bbzn.user.client.dataobject.User;
import com.bbzn.user.client.service.CommonService;
import com.bbzn.user.client.service.SimpleMongoService;
import com.bbzn.user.client.service.UserService;
import com.bbzn.user.client.utils.OrderNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Slf4j
@Order
@Aspect
@Component
public class OperationAspect {

    @Autowired
    private SimpleMongoService simpleMongoService;

    @Autowired
    private CommonService commentService;

    @Autowired
    private UserService userService;

    @After("@annotation(com.bbzn.user.client.aspect.OperationAnnotation)")
    public void OperationHandler(JoinPoint joinPoint) throws Exception {
        log.info("开始记录操作日志》》》》》》》》》》》》》");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String imei = request.getParameter("imei");

        // EMQ调用
        String header = request.getHeader(RedisConstant.HEADER);
        if (StringUtils.isEmpty(header)) {
            return;
        }
        Long userId = commentService.getUserId(request);
        User user = userService.selectByPrimaryKey(userId);
        OptHis operation = new OptHis();
        // 获取注解
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        OperationAnnotation operationAnnotation = method.getAnnotation(OperationAnnotation.class);
        operation.setOptContent(operationAnnotation.content());
        if(StringUtils.isNotEmpty(imei)){
            operation.setImei(imei);
        }
        if(null!=userId && userId!=0){
            operation.setUserId(userId);
        }
        operation.setId(OrderNoUtil.getOrderNo());
        operation.setCreateTime(new Date());
        operation.setUpdateTime(new Date());
        operation.setCreatedBy(user.getUsername());
        operation.setUpdatedBy(user.getUsername());
        operation.setCreatedBy(user.getUsername());
        // 插入数据库
        simpleMongoService.insert(operation, MongoDbConstant.OPT_HIS);

        log.info("结束记录操作日志》》》》》》》》》》》》》");

    }
}
